/**
 * ExportWord.java
 * Version 1.0.0
 * Created on 2018年7月28日
 */
package com.yincheng.oes.business.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author yangsy
 * @version 1.0.0
 * @since 创建时间：2018年7月28日
 * <p>
 * 功能描述：导出word文档工具类
 * <p>
 * 邮箱地址：446948434@qq.com
 */
public class ExportWord {

    // private static String path = ExportWord.class.getResource("/").getPath();
    // private static File parentFile = new File(path).getParentFile().getParentFile();
    //// private static String parentPath = parentFile.getAbsolutePath()+"/WEB-INF/classes/template";
    // private static String parentPath = parentFile.getAbsolutePath()+"/src/main/resources/template";
    private static File temFile = new File(Consists.parentPath);

    public static void exportWord(Map<String, Object> dataMap, String templateName, OutputStream os)
            throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setDefaultEncoding("utf-8");
        configuration.setDirectoryForTemplateLoading(temFile);

        Template template = configuration.getTemplate(templateName, "utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8), 40960);

        template.process(dataMap, out);
        out.flush();
        out.close();
    }

}
